package com.cheng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cheng.common.exception.CommonException;
import com.cheng.common.utils.StringUtil;
import com.cheng.data.entity.PermInfoEntity;
import com.cheng.data.mapper.PermInfoMapper;
import com.cheng.data.vo.PermInfoVO;
import com.cheng.data.vo.RoleInfoVO;
import com.cheng.service.PermInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: ChengJW
 * @date: 2021/11/4 7:41
 * @version: 1.0
 * @description: 权限处理
 */
@Service
public class PermInfoServiceImpl implements PermInfoService {


    private final static Logger logger = LoggerFactory.getLogger(PermInfoServiceImpl.class);

    @Autowired
    private PermInfoMapper permInfoMapper;

    /**
     * @author: ChengJW
     * @params: []
     * @return: java.util.List<com.cheng.data.entity.PermInfoEntity>
     * @date: 2021/11/04
     * @description: 通过权限名称，聚合查询权限信息
     */
    @Override
    public List<PermInfoVO> selectPermInfoByPrNm(String urNm) throws CommonException {

        //查询1级菜单，查询权限信息
        Map<String,Object> map = new HashMap<>();
        map.put("urNm",urNm);
        map.put("prNm","sys%");
        return permInfoMapper.queryPermInfo(map);
    }

    /**
     * @author: ChengJW
     * @params: []
     * @return: java.util.List<com.cheng.data.entity.PermInfoEntity>
     * @date: 2021/11/04
     * @description: 查询下一级菜单
     */
    @Override
    public List<PermInfoVO> selectPermInfoByPrUpsLevelNm(String prUpsLevelNm,String urNm) throws CommonException{

        //查询下一级菜单权限
        Map<String,Object> map = new HashMap<>();
        map.put("urNm",urNm);
        map.put("prUpsLevelNm",prUpsLevelNm);
        return permInfoMapper.queryPermInfo(map);
    }

    /**
     * @author: ChengJW
     * @params: []
     * @return: java.util.List<com.cheng.data.vo.PermInfoVO>
     * @date: 2021/11/13
     * @description: 查询所有权限信息
     */
    @Override
    public List<PermInfoEntity> queryPermInfoList(PermInfoVO permInfoVO) throws CommonException {

        QueryWrapper<PermInfoEntity> wrapper = new QueryWrapper<>();
        //菜单名称(中文描述)
        wrapper.like(StringUtil.isNotNull(permInfoVO.getPrDesc()),"pr_Desc",permInfoVO.getPrDesc());
        //一级菜单代码
        wrapper.like(StringUtil.isNotNull(permInfoVO.getPrUpsLevelNm()),"pr_Ups_Level_Nm",permInfoVO.getPrUpsLevelNm());
        //二级菜单代码
        wrapper.like(StringUtil.isNotNull(permInfoVO.getPrLevelNm()),"pr_Level_Nm",permInfoVO.getPrLevelNm());
        wrapper.orderByDesc("pr_Ups_Level_Nm");
        return permInfoMapper.selectList(wrapper);
    }

    /**
    * @author: ChengJW
    * @params: [prLvl, txTpCd]
    * @return: java.util.List<com.cheng.data.entity.PermInfoEntity>
    * @date: 2021/11/15
    * @description: 查看所有的菜单权限信信息
    */
    @Override
    public List<PermInfoEntity> queryPermListForTree(String prLvl, String txTpCd) throws CommonException {
        // txTpCd 01 表示一级菜单信息查询；02 表示二级菜单查询
        QueryWrapper<PermInfoEntity> wrapper = new QueryWrapper<>();
        if ("01".equals(txTpCd)){
            wrapper.like("pr_Nm","sys");
            wrapper.isNull("pr_Level_Nm");
        }else if ("02".equals(txTpCd)){
            wrapper.eq("pr_Ups_Level_Nm",prLvl);
        }
        wrapper.eq("pr_Del_Id","0").orderByAsc("pr_Id");
        return permInfoMapper.selectList(wrapper);
    }
}
